package io.mft.commons.db.mapper.proxyinvoker.invoker;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.db.handler.EntityListHandler;
import io.mft.commons.db.ISQLParameter;
import io.mft.commons.db.SQL;
import io.mft.commons.db.mapper.SQLQueryMapResult;
import io.mft.commons.db.mapper.builder.ParameterWrapper;
import io.mft.commons.db.mapper.proxyinvoker.IProxyInvoker;
import io.mft.commons.function.response.kv.KVMap;
public class SQLQueryMapResultProxyInvoker implements IProxyInvoker<SQLQueryMapResult>{

	@Override
	public SQLQueryMapResult invoke(Method method, Session session, ISQLParameter sqlParameter, KVMap parameter,
			ParameterWrapper methodArgs) throws Exception{
		SQL sql=sqlParameter.sqlText(parameter);
		List<Entity> entities=session.query(sql.getSql(), new EntityListHandler(true), sql.getArgs());
		List<KVMap> dataList=new ArrayList<KVMap>();
		entities.forEach(e->{
			KVMap kvMap=new KVMap();
			kvMap.putAll(e);
			dataList.add(kvMap);
		});
		SQLQueryMapResult sqlQueryMapResult=new SQLQueryMapResult(dataList);
		return sqlQueryMapResult;
	}
	
}
